<?

include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');
include($_SERVER['DOCUMENT_ROOT'].'/filter.libs.php');

$bits = explode('.',$_SERVER['SERVER_NAME']);
$domain = $bits[0];

$row = getRow("SELECT portal_id,title,user_id,realname,readable FROM portal INNER JOIN user USING (user_id) WHERE domain = ".dbQuote($domain)." AND status_id = 1");

if (empty($row['portal_id'])) {
	header("HTTP/1.0 404 Not Found");
	print "<html><head>";
	print "<title>Not found!</title>";
	print "</head><body>";
	print "<p>The page you are looking for is not available</p>";
	
	print "<p><a href=\"http://www.geographs.org/portals/?ref=$domain\">Try the Portal Homepage</a></p>";
	print "</body></html>";
	print str_repeat(" ",500);
	exit;
}

$portal_id = $id = intval($row['portal_id']);
$title = $row['title'];

$row['count'] = count_active($id);

$page_title = "similarity prototype";
include($_SERVER['DOCUMENT_ROOT'].'/header.inc.php');



print "<h2>Visual similarity prototype</h2>";

if (empty($_GET['id']) || intval($_GET['id'] <= 1)) {

	$id = getOne("SELECT l.gridimage_id FROM libpuzzlesig l INNER JOIN p2.gridimage{$portal_id}_bi_view LIMIT ".rand(0,$row['count']-1).",1");
} else {
	$id = intval($_GET['id']);
}
if (empty($id)) {
	die("no image found");
}
$words = getCol("SELECT word FROM libpuzzleword WHERE gridimage_id = $id");

if (empty($words)) {
	die("image not in portal, or otherwise not found");
}


$images = getAssoc("SELECT gridimage_id,COUNT(*) c FROM libpuzzleword INNER JOIN p2.gridimage{$portal_id}_bi_view USING (gridimage_id) WHERE `word` IN (".implode(',',$words).") GROUP BY gridimage_id ORDER BY c DESC LIMIT 40");


if (empty($images) || count($images) < 2) {
	die("no similar images found - sorry");
}
if (count($images) == 40) {
	print "<p>Top 40 similar images shown. ";
	
} else {
	print "<p>".count($images)." image similar images found. ";
}

print " load <a href=\"?id=rand\">Another Random image</a>";

	$ids = array_keys($images);
		
	$imagedata = getAssoc("SELECT gridimage_id,grid_reference,title,realname,user_id FROM geograph_live.gridimage_search WHERE gridimage_id IN (".implode(',',$ids).")");
		
	foreach ($images as $gid => $count) {
		$c = $imagedata[$gid];
		if ($c) {
			$c['gridimage_id'] = $gid;
		?>
		  <div style="float:left;position:relative; width:150px; height:150px; padding:2px; text-align:center; border:1px solid silver">
		<?=$count ?>
		  <div align="center" style="width:120px;margin-left:auto;margin-right:auto" class="hoverimage" id="<? echo $id; ?>">
		  <a title="<? ehec('grid_reference'); ?> : <? ehec('title'); ?> by <? ehec('realname'); ?> - click to view full size image" href="http://www.geograph.org.uk/photo/<? echo $c['gridimage_id']; ?>" style="border:0"><img src="<? echo gGUc($c); ?>"/></a></div>
		  <a href="?id=<? echo $c['gridimage_id']; ?>">similar</a>
	 	 </div>
		<?
		}

	}
	print "<br style=\"clear:both\"/>";


	
	
include($_SERVER['DOCUMENT_ROOT'].'/portals/footer.inc.php');
